package com.reggie.controller.admin;

import com.reggie.dto.CategoryDTO;
import com.reggie.dto.CategoryPageQueryDTO;
import com.reggie.entity.Category;
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类管理接口")

public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //分页 分类查询
    @GetMapping("/page")
    @ApiOperation("分页分类查询")
    public R<PageResult> page(CategoryPageQueryDTO pageQueryDTO){
    PageResult pageResult = categoryService.pageQuery(pageQueryDTO);
    return R.success(pageResult);
    }


     //新增菜品分类
    @PostMapping
    @ApiOperation("新增分类")
    public R<String> save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类{}",categoryDTO);
        categoryService.save(categoryDTO);
        return R.success();
    }

    //删除分类
    @DeleteMapping
    @ApiOperation("根据id删除指定分类")
    public R<String> deleteById(Long id){
        categoryService.deleteById(id);
        return R.success();
    }

    //修改分类管理
    @PutMapping
    @ApiOperation("修改分类")
    public R<String> updateById(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateById(categoryDTO);
        return R.success();
    }

    //禁用启用
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用")
    public R<String> startOrStop(@PathVariable Integer status,Long id){
        categoryService.startOrStop(status,id);
        return R.success();
    }

    //查询分类下的所有资源
    @GetMapping("/list")
    @ApiOperation("查询分类下的资源")
    public R<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return R.success(list);
    }


}
